<template>
  <router-view></router-view>
</template>

<script>
import jwt_decode from "jwt-decode";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
export default {
  name: "App",
  setup() {
    const router = useRouter();
    const store = useStore();
    const access = localStorage.getItem("access");

    if (!access) {
      localStorage.clear();
      router.push({ name: "login" });
      return;
    }

    const acc = jwt_decode(access);
    if (acc.exp * 1000 < new Date().getTime()) {
      localStorage.clear();
      router.push({ name: "login" });
      return;
    }

    const userInfo = localStorage.getItem("userInfo");

    if (access && userInfo) {
      store.commit("setAccess", access);
      store.commit("setUserInfo", JSON.parse(userInfo));
    }
  },
};
</script>

<style scoped>
</style>
